package document.controller;

import document.dao.DocumentDao;
import document.entity.Document;
import org.json.JSONObject;

import javax.servlet.ServletException;
import javax.servlet.http.HttpServlet;
import javax.servlet.http.HttpServletRequest;
import javax.servlet.http.HttpServletResponse;
import java.io.IOException;

public class DocumentCheckServlet extends HttpServlet {
    private final DocumentDao documentDao = new DocumentDao();
    private static final long serialVersionUID = 1;

    public DocumentCheckServlet() {
        super();
    }

    protected void doGet(HttpServletRequest request, HttpServletResponse response)
            throws IOException, ServletException {
        doPost(request, response);
    }

    /**
     * 对文档进行审核或者更新审核
     * @param request
     * @param response
     * @throws IOException
     * @throws ServletException
     */
    protected void doPost(HttpServletRequest request, HttpServletResponse response)
            throws IOException, ServletException {
        System.out.println("进入了DocumentCheckServlet");
        request.setCharacterEncoding("utf-8");
        response.setContentType("text/html;charset=utf-8");
        Document document = new Document();
        int document_id =Integer.parseInt(request.getParameter("document_id"));
        String review = request.getParameter("review");
        int score = Integer.parseInt(request.getParameter("score"));
        document.setId(document_id);
        document.setReview(review);
        document.setScore(score);
        documentDao.checkDocument(document);

        System.out.println("执行了文档审核!");
        JSONObject jsonObject = new JSONObject();
        jsonObject.put("code",200);
        jsonObject.put("message","文档审核成功");
        System.out.println(jsonObject.toString());
        response.getWriter().write(jsonObject.toString());
    }
}
